import Vapor
import FluentSQLite
import FluentPostgreSQL
import WebSocket
import Authentication
import APNS

/// Called before your application initializes.
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
    
    services.register(NIOServerConfig.default(hostname: "0.0.0.0", port: 8181))
    /// Register providers first
    try services.register(FluentSQLiteProvider())
    //    try services.register(FluentPostgreSQLProvider())
    try services.register(AuthenticationProvider())
    services.register(APNS.self)
    services.register(HeadersMiddleware.self)
    
    /// Register contnet config
    var contentConfig = ContentConfig.default()
    try content(config: &contentConfig)
    services.register(contentConfig)
    
    /// Register middleware
    var middlewaresConfig = MiddlewareConfig() // Create _empty_ middleware config
    try middlewares(config: &middlewaresConfig)
    services.register(middlewaresConfig)

    /// Register the configured SQLite database to the database config.
    var databasesConfig = DatabasesConfig()
    try databases(config: &databasesConfig)
    services.register(databasesConfig)

    /// Configure migrations
    var migrationsConfig = MigrationConfig()
    try migrate(migrations: &migrationsConfig)
    services.register(migrationsConfig)
    
    /// Register routes to the router
    let router = EngineRouter.default()
    try routes(router)
    services.register(router, as: Router.self)
    
    let io = NIOWebSocketServer.default()
    try sockets(server: io)
    services.register(io, as: WebSocketServer.self)    
}
